package com.example.mcu.serial;

import java.util.Timer;
import java.util.TimerTask;

import java.util.ArrayList;


import android.annotation.SuppressLint;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;

import android.text.format.Time;
import android.widget.Toast;

import android.util.Log;




public class SerialHandlerService extends Service {
	
	

	public static final byte M_POWER_CHARGE = (byte) 0x6B; // 电池充电 MCU 0x81
	public static final byte M_POWRR_FULL = (byte) 0x82; // 电池充满 MCU 0x82
	public static final byte M_POWER_PERCENT = (byte) 0x6D; // 电池电量百分比 MCU 0x83
	public static final byte M_POWER_EXTER_IN = (byte) 0x6E; // 外置电源接入 MCU 0x84
	
	public static final byte M_VOLUM_FIND = (byte) 0x85; // MCU查询音量大小 MCU 0x85
	public static final byte M_FIND_S_SATATE = (byte) 0x86; // MCU查询上位机工作状态 MCU
															// 0x86
	public static final byte M_VOLUM_UP = (byte) 0x87; // 音量+
	public static final byte M_VOLUM_DOWN = (byte) 0x88; // 音量+
	public static final byte M_SHIFT = (byte) 0x89; // 播放状态改变
	public static final byte M_PREVIOUS = (byte) 0x8A; // 上一首
	public static final byte M_NEXT = (byte) 0x8B; // 下一首
	public static final byte M_SPEECH_START = (byte) 0x8D; // 语音开始
	public static final byte M_SPEECH_END = (byte) 0x8E; // 语音 结束
	public static final byte S_HARDWARE_VERSION = (byte) 0x61; // 获取MCU软硬件版本号
	public static final byte M_SPEECH_HOLD=        (byte)0x8F;  //	语音 结束	
	
	public static final byte S_MICROWAVE       = (byte) 0x70; // 微波感应 近
	
	public static final byte M_VOLUM_NUMBER       = (byte) 0x6A; // 查询音量
	public static final byte M_BOOLOADER       = (byte) 0x72; // 回复
	
	public boolean b = false;
	Serial serialEngine;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 1:
				Pack p = (Pack) msg.obj;
				// Toast.makeText(SerialHandlerService.this,
				// byteToString(p.tobytes()), Toast.LENGTH_SHORT).show();
				if (p.getType() == (byte) 0x00) {
				} else {
					
					
					handlerpack2(p);
				}
				break;
			default:
				break;
			}
		};
	};

	static int i = 0;

	@Override
	public void onCreate() {
		super.onCreate();
		serialEngine = Serial.getInstance();
		serialEngine.setHandler(handler);
		serialEngine.start();
		
		

		
	
		TimerTask task=new TimerTask() {
			@Override
			public void run() {
				SerialSend.getInstance().sendStatus(SerialSend.Serial_POWER_PERCENT);
			}
		};
		
		Timer time=new Timer();
		time.schedule( task, 0, 60*1000);


	}

	


	@SuppressLint("DefaultLocale")
	public static String byteToString(byte[] b) {
		StringBuffer buffer = new StringBuffer(b.length);
		for (int i = 0; i < b.length; i++) {
			String strHex = Integer.toHexString(b[i]).toUpperCase();

			if (strHex.length() > 3) {
				buffer.append(strHex.substring(6) + " ");
			} else if (strHex.length() < 2) {
				buffer.append("0" + strHex + " ");
			} else {
				buffer.append(strHex + " ");
			}
		}
		return buffer.toString();
	}

	
	public void handlerpack2(Pack p){
		 Intent intent=new Intent();
		 Bundle bundle=new Bundle();
		//Toast.makeText(SerialHandlerService.this, ""+byteToString(p.tobytes()), Toast.LENGTH_SHORT).show();
		switch (p.getType()) {
		case M_POWER_CHARGE:
			   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_POWER_CHARGE"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
			break;
	    case M_POWRR_FULL:
			   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_POWRR_FULL"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
			break;
	    case M_POWER_PERCENT:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_POWER_PERCENT"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_POWER_EXTER_IN:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_POWER_EXTER_IN"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_VOLUM_FIND:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_VOLUM_FIND"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_FIND_S_SATATE:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_FIND_S_SATATE"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_VOLUM_UP:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_VOLUM_UP"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	  
	    case M_VOLUM_DOWN:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_VOLUM_DOWN"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_SHIFT:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_SHIFT"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_PREVIOUS:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_PREVIOUS"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_NEXT:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_NEXT"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;	
	    case M_SPEECH_START:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_SPEECH_START"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
			break;
	    case M_SPEECH_END:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_SPEECH_END"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
		    
	     case S_HARDWARE_VERSION:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "S_HARDWARE_VERSION"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
		    
	    case S_MICROWAVE:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "S_MICROWAVE"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
		    
		    
	    case M_VOLUM_NUMBER:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_VOLUM_NUMBER"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    case M_BOOLOADER:
	    	   intent.setAction("cozon.moco");
	           intent.putExtra("Msg", "M_BOOLOADER"); 
	           bundle.putSerializable("pack", p);
	           intent.putExtras(bundle);
	           SerialHandlerService.this.sendBroadcast(intent);
		    break;
	    
		default:
			break;
	}		
		
	}
	 //需用内部类继承Binder,并定义方法获取Service对象  
    private final IBinder binder = new MyBinder();  
    public class MyBinder extends Binder {  
    	public SerialHandlerService getService() {  
            return SerialHandlerService.this;  
        }  
    }  
    @Override 
    public IBinder onBind(Intent intent) {//当客户端调用bindService()方法时调用此函数  
        return binder;  
    }  
  
   
	
}
